<template>
  <div class="staff-container">
  <el-card shadow="never">
    <div class="toolbar">
      <el-input v-model="search" placeholder="搜索姓名/账号/联系方式" style="width: 200px;" @keyup.enter="fetchList" clearable />
      <el-select v-model="role" placeholder="全部角色" clearable style="width: 140px; margin-left: 10px;">
        <el-option label="管理员" value="manager" />
        <el-option label="摄影师" value="employee" />
      </el-select>
      <el-select v-model="gender" placeholder="全部性别" clearable style="width: 140px; margin-left: 10px;">
        <el-option label="男" value="男" />
        <el-option label="女" value="女" />
      </el-select>
      <el-button type="primary" @click="handleAdd" style="margin-left: 10px;">新增员工</el-button>
    </div>
    
    <el-table :data="tableData" border stripe :header-cell-style="{ background: '#faf5fe', color: '#6f21c8' }"  height="calc(100vh - 240px)" v-loading="loading">
      <el-table-column prop="photographerId" label="ID" width="60" align="center" />
      <el-table-column prop="avatarUrl" label="头像" width="80" align="center">
        <template #default="{ row }">
          <el-avatar 
            v-if="row.avatarUrl" 
            :src="fileBaseUrl() + row.avatarUrl" 
            :size="50"
            style="border-radius: 50%;"
          />
          <el-avatar v-else :size="50" style="background-color: #f0f0f0;">
            <el-icon><User /></el-icon>
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="100" />
      <el-table-column prop="gender" label="性别" width="60" align="center" />
      <el-table-column prop="contactInfo" label="联系方式" width="120" />
      <el-table-column prop="expertise" label="擅长领域" min-width="150" show-overflow-tooltip />
      <el-table-column prop="account" label="账号" width="120" />
      <el-table-column prop="role" label="角色" width="80" align="center">
        <template #default="{ row }">
          <el-tag :type="row.role === 'manager' ? 'danger' : 'primary'">
            {{ row.role === 'manager' ? '管理员' : '摄影师' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" min-width="140" />
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
          <el-button type="warning" link @click="handleResetPassword(row)">重置密码</el-button>
          <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchList"
        @current-change="fetchList"
      />
    </div>
    
    <PhotographerEditDialog
      v-model:visible="editDialogVisible"
      :photographer="currentPhotographer"
      :is-edit="isEdit"
      @submit="handleSubmit"
    />
  </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User } from '@element-plus/icons-vue'
import PhotographerEditDialog from './PhotographerEditDialog.vue'
import { getPhotographerList, addPhotographer, updatePhotographer, deletePhotographer, resetPassword } from '@/apis/admin/photographer'
import { fileBaseUrl } from '@/apis/files'

const tableData = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const search = ref('')
const role = ref('')
const gender = ref('')

const editDialogVisible = ref(false)
const isEdit = ref(false)
const currentPhotographer = ref({})

const fetchList = async () => {
  loading.value = true
  try {
    const res = await getPhotographerList({
      current: currentPage.value,
      size: pageSize.value,
      search: search.value,
      role: role.value,
      gender: gender.value
    })
    if (res.code === '0') {
      tableData.value = res.data.records
      total.value = res.data.total
    }
  } catch (e) {
    ElMessage.error('获取员工列表失败')
  } finally {
    loading.value = false
  }
}

// 监听搜索和筛选条件变化
watch([search, role, gender], () => {
  currentPage.value = 1
  fetchList()
})

const handleAdd = () => {
  isEdit.value = false
  currentPhotographer.value = {}
  editDialogVisible.value = true
}

const handleEdit = (row) => {
  isEdit.value = true
  currentPhotographer.value = { ...row }
  editDialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该员工吗？', '提示', { type: 'warning' })
    .then(async () => {
      const res = await deletePhotographer(row.photographerId)
      if (res.code === '0') {
        ElMessage.success('删除成功')
        fetchList()
      } else {
        ElMessage.error(res.msg || '删除失败')
      }
    })
}

const handleResetPassword = (row) => {
  ElMessageBox.confirm(`确定要重置 ${row.name} 的密码吗？`, '提示', { type: 'warning' })
    .then(async () => {
      const res = await resetPassword(row.photographerId)
      if (res.code === '0') {
        ElMessage.success('密码重置成功')
      } else {
        ElMessage.error(res.msg || '密码重置失败')
      }
    })
}

const handleSubmit = async (form) => {
  loading.value = true
  try {
    const api = isEdit.value ? updatePhotographer : addPhotographer
    const res = await api(form)
    if (res.code === '0') {
      ElMessage.success(isEdit.value ? '编辑成功' : '新增成功')
      editDialogVisible.value = false
      fetchList()
    } else {
      ElMessage.error(res.msg || '操作失败')
    }
  } catch (e) {
    ElMessage.error('操作失败')
  } finally {
    loading.value = false
  }
}

onMounted(fetchList)
</script>

<style scoped>
.staff-container {
  padding: 20px;
}
.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>